/*TCSS 360 ConferenceGuru
 * 
 * Author: Tyler Simrell
 * 
 * Edited by: Lawrence Grass - 11/27 adding observable features.
 */
package model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Observable;

public class ProgramChair extends Observable  implements Serializable
{
	
	private static final long serialVersionUID = -1456237194349425707L;
	private List<ConferencePaper> submittedPapers;
	private final String myName;
	private Map<String, SubProgramChair> subprogramChairs;
	private Map<String, Reviewer> reviewers;
	
	public ProgramChair(List<ConferencePaper> the_papers, 
			            Map<String, SubProgramChair> the_subprogramChairs,
			            Map<String, Reviewer> the_reviewers, String the_name)
	{
		submittedPapers = the_papers;
		myName = the_name;
		subprogramChairs = the_subprogramChairs;
		reviewers = the_reviewers;
	}
	
	//Once conference paper is finished, loop through the paper to find the selected paper, then set the boolean
	//to true for the setDecision flag. The sent in parameter 'the_decision' is what the 
	//program chair wants to do with the paper (accept, decline, or undecided). 
	public void setDecision(String the_paper_title, String the_decision)
	{
		for(ConferencePaper tempPaper: submittedPapers)
		{
			if(the_paper_title.equals(tempPaper.getTitle()))
			{
				tempPaper.setAcceptance(the_decision);
				updateObserver();
			}
		}
	}
	
	//This method adds a paper to the subprogram chair's list of assigned papers.
	public void assignManuscript(SubProgramChair the_subchair, ConferencePaper the_paper)
	{
		the_subchair.addManuscipt(the_paper);
		updateObserver();
	}
	
	//This method creates a new Subprogram Chair and puts it into the map, along with the passed in name.
	public void designateSubprogramChair(String the_user)
	{
		SubProgramChair temp = new SubProgramChair(new ArrayList<ConferencePaper>());
		temp.setName(the_user);
		subprogramChairs.put(the_user, temp);
		updateObserver();
	}
	
	//This method loops through the list of papers, give a new list references to the papers that are accepted (as in
	//the paper has a "yes" status.
	public List<ConferencePaper> submitAcceptanceList()
	{
		List<ConferencePaper> acceptedPapers = new ArrayList<ConferencePaper>();
		for(ConferencePaper tempPaper: submittedPapers)
		{
			if("yes".equals(tempPaper.getAcceptanceStatus()))
			{
				submittedPapers.add(tempPaper);
				updateObserver();
			}
		}
		return acceptedPapers;
	}
	
	//This method loops through the list of submittedPapers to find the paper named by the passed in string, then
	//returns the reviews attached to it. If no reviews are attached, this method will return a null.
	public List<Review> getReview(String the_paper_title)
	{
		
		List<Review> paperReviews = null;
		for(ConferencePaper tempPaper: submittedPapers)
		{
			if(the_paper_title.equals(tempPaper.getTitle()))
			{
				paperReviews = tempPaper.getReviews();
				updateObserver();
				break;
			}
		}
		return paperReviews;
	}
	
	//This method returns the list of all the submitted papers.
	public List<ConferencePaper> getPapers()
	{
		return submittedPapers;
	}

	//This method designates a user to be a reviewer. 
	public void designateReviewer(String the_user)
	{
	    Reviewer temp = new Reviewer(the_user);
	    reviewers.put(the_user, temp);
		updateObserver();
	}
	
	//This method returns the name of the Program Chair.
	public String getName()
	{
		return myName;
	}
	
	/*
	 * This method returns the map of subprogramChairs.
	 */
	public Map<String, SubProgramChair> getSubprogramChairs()
	{
		return subprogramChairs;
	}
	
	//This method returns the reviewers in the conference.
	public Map<String, Reviewer> getReviewers()
	{
		return reviewers;		
	}
	
	//This method updates the observers.
	//Added by Lawrence Grass.
	private void updateObserver()
	{
		setChanged();
		notifyObservers(this);
	}

}
